package main

import (
    "core"
    "ttf"
    "sdl"
    "fmt"
)

var Screen *sdl.Surface = nil;

func main() {

    if sdl.Init(sdl.INIT_VIDEO) != 0 {
        panic(sdl.GetError())
    }

    if ttf.Init() != 0 {
        panic("ttf error")
    }

    defer sdl.Quit()

    var font *ttf.Font = ttf.OpenFont("./mplus-1c-medium.ttf", 24)
    defer font.Close()

    var screen = sdl.SetVideoMode(640, 480, 32, 0)
    if screen == nil {
        panic(sdl.GetError())
    }
    Screen = screen

    var fpsTimer *core.FPSTimer = &core.FPSTimer{}
    fpsTimer.SetFPS(60)

    sdl.WM_SetCaption("発電攻撃", "")

    var lastfps uint32 = 0
    var fpstext *sdl.Surface = nil

    var gamemodemgr *core.GameModeMgr = new(core.GameModeMgr)
    gamemodemgr.SetGameMode(&BoxDrawTest{})

    for {
        e:= &sdl.Event{}

        for e.Poll() {
            switch e.Type {
            case sdl.QUIT:
                return
            case sdl.KEYDOWN:
                if e.Keyboard().Keysym.Sym == sdl.K_ESCAPE {
                    return
                }
            default:
            }
        }

        screen.FillRect(nil, 0x000000)

	gamemodemgr.Update()

        var nowfps uint32 = fpsTimer.GetRealFPS()
        if lastfps != nowfps {
		fpstext.Free()
		fpstext = ttf.RenderUTF8_Blended(font, fmt.Sprintf("FPS:%d", nowfps), sdl.Color{0xFF, 0xFF, 0xFF, 0xFF})
        }
        lastfps = nowfps
        if fpstext != nil {
            screen.Blit(&sdl.Rect{10,10,0,0}, fpstext, nil)
        }

        fpsTimer.WaitFrame()
        screen.Flip()
    }
}

